package com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.WritableFont;
import jxl.write.WriteException;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.json.JSONArray;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.bo.ITXingzhengshouwendengjibuBO;
import com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.vo.TXingzhengshouwenNeirong;
import com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.vo.TXingzhengshouwendengjibu;
import com.tjsinfo.tjsoa.mail.vo.TjsAnnex;
import com.tjsinfo.tjsoa.system.dao.ISystemSpringDao;

public class DengJiBuAction extends DispatchAction{
	private ITXingzhengshouwendengjibuBO xingzhengshouwendengjibuBO;
	private ISystemSpringDao springDao;


	public void setSpringDao(ISystemSpringDao springDao) {
		this.springDao = springDao;
	}
	 
	
	public ITXingzhengshouwendengjibuBO getXingzhengshouwendengjibuBO() {
		return xingzhengshouwendengjibuBO;
	}

	public void setXingzhengshouwendengjibuBO(
			ITXingzhengshouwendengjibuBO xingzhengshouwendengjibuBO) {
		this.xingzhengshouwendengjibuBO = xingzhengshouwendengjibuBO;
	}

	private IQbcDao qbcDao;
	
	public IQbcDao getQbcDao() {
		return qbcDao;
	}

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}
	public ActionForward allList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		List<TXingzhengshouwendengjibu> list = xingzhengshouwendengjibuBO.selectAll();
		request.setAttribute("list", list);
		return mapping.findForward("ok");
	}
	public ActionForward dengjibuSearch(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws IOException, WriteException {
		jxl.write.WritableWorkbook wwb = null;
		try {
			String beginNO=request.getParameter("beginNO");
			String endNO=request.getParameter("endNO");
			String yearNo=request.getParameter("yearNO");
			String doctype=request.getParameter("doctype");
			doctype=URLDecoder.decode(URLDecoder.decode(doctype,"UTF-8"),"UTF-8");  
			
			
			String whereSQL="";
			if (beginNO!=null && !"".equals(beginNO)) {
				whereSQL+="shouwenbianhao>="+beginNO+" and  ";
			}
			if (endNO !=null && !"".equals(endNO)) {
				whereSQL+="shouwenbianhao<="+endNO+" and ";
			}
			if (yearNo !=null && !"".equals(yearNo) && doctype !=null && !"".equals(doctype)) {
				whereSQL+="shouwentype='"+doctype+yearNo+"' and ";
			}
	//		List<TXingzhengshouwenNeirong> shouwenList=qbcDao.getCriteria(criteria);
			List<Map<String, String>> tempList = springDao.commonSelect(new String[]{
					"shouwenbianhao","shouwenriqi","laiwenzihao","laiwenjiguan","zhaiyou"},
					"SELECT shouwenbianhao, shouwenriqi, laiwenzihao, laiwenjiguan, zhaiyou FROM t_xingzhengshouwen_neirong where "
					+whereSQL+" flowid <> 0 order by shouwenbianhao");

			JSONArray array = new JSONArray();
			JSONObject object = new JSONObject();
			for (Map<String, String> map : tempList) {
				JSONObject json = new JSONObject();
				json.put("shouwenbianhao", doctype+yearNo+"年"+map.get("shouwenbianhao")+"号");
				json.put("shouwenriqi", map.get("shouwenriqi").substring(0, 10));
				json.put("laiwenzihao", map.get("laiwenzihao"));
				json.put("laiwenjiguan", map.get("laiwenjiguan"));
				json.put("zhaiyou", map.get("zhaiyou"));
				array.put(json);
			}

			object.put("root", array);
			response.getWriter().print(object);

//			String uuid=ToolKit.getUUID();
//			File f = new File("C:\\tempFile\\"+uuid+".xls");
//			wwb = Workbook.createWorkbook(f);
//			jxl.write.WritableSheet ws = wwb.createSheet("查询结果", 0);
//			ws.addCell(new jxl.write.Label(0,0, "收文编号"));
//			ws.addCell(new jxl.write.Label(1,0, "来文日期"));
//			ws.addCell(new jxl.write.Label(2,0, "来文字号"));
//			ws.addCell(new jxl.write.Label(3,0, "来文机关"));
//			ws.addCell(new jxl.write.Label(4,0, "摘要"));
//			ws.addCell(new jxl.write.Label(5,0, "签收"));
//			ws.addCell(new jxl.write.Label(6,0, "备注"));
//			for (int i=0,j=1;i<shouwenList.size();i++,j++) {
//				ws.addCell(new jxl.write.Label(0,j, shouwenList.get(i).getShouwenbianhao()));
//				ws.addCell(new jxl.write.Label(1,j, shouwenList.get(i).getLaiwenriqi().toString().substring(0,10)));
//				ws.addCell(new jxl.write.Label(2,j, shouwenList.get(i).getLaiwenzihao()));
//				ws.addCell(new jxl.write.Label(3,j, shouwenList.get(i).getLaiwenjiguan()));
//				ws.addCell(new jxl.write.Label(4,j, shouwenList.get(i).getZhaiyou()));
//				ws.addCell(new jxl.write.Label(5,j, ""));
//				ws.addCell(new jxl.write.Label(6,j, ""));
//			}
//			
//
//			jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.ARIAL, 20, WritableFont.BOLD, false,UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.GREEN);
//			jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc);
//			wcfFC.setBackground(jxl.format.Colour.RED);
//			wwb.write();
//			wwb.close();
//			
//			request.setAttribute("uuid", uuid);
			return null;
			
		} catch (Exception e) {
			wwb.write();
			wwb.close();
			e.printStackTrace();
			return null;
		}
	}
	public ActionForward downLoad(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String url = "error";
		OutputStream toClient = null;
		try {
			String filename = request.getParameter("uuid");
			File file = new File("C:\\tempFile\\"+filename+".xls"); 	
			InputStream fis = new BufferedInputStream(new FileInputStream("C:\\tempFile\\"+filename+".xls"));
			byte[] buffer = new byte[fis.available()];
			fis.read(buffer);
			fis.close();		
			response.reset();
		    response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename+".xls"));
	        response.addHeader("Content-Length", "" + file.length());
	        toClient = new BufferedOutputStream(response.getOutputStream());
	        response.setContentType("application/octet-stream");
	        toClient.write(buffer);
	        toClient.flush();
	        toClient.close();
			url = "ok";
		} catch (IOException ex) {
			request.setAttribute("returnSting", "服务器忙，请稍后下载！");
			ex.printStackTrace();
			return mapping.findForward("error");
		} catch(NullPointerException ex){
			return null;
		} catch (Exception ex){
			request.setAttribute("returnSting", "服务器忙，请稍后下载！");
			return mapping.findForward("error");
		}finally{
	        toClient.close();
			return mapping.findForward("error");
		}
	}
	
}
